package com.penglecode.dp.structural.proxy;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
/**
 * 动态代理
 * 
 * @author pengpeng
 * @date 2013-6-18下午3:23:04
 * @version 1.0
 * @see [相关类/方法]
 * @since [产品/模块版本]
 */
public class JdkProxy implements InvocationHandler {

	private static final Log logger = LogFactory.getLog(ProxyUserService.class);
	
	private Object target;
	
	public JdkProxy(Object target) {
		super();
		if(target == null){
			throw new IllegalArgumentException("'target' parameter can not be null!");
		}
		this.target = target;
	}
	
	@SuppressWarnings("unchecked")
	public <T> T getProxy(){
		return (T) Proxy.newProxyInstance(Thread.currentThread().getContextClassLoader(), target.getClass().getInterfaces(), this);
	}

	public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
		if(args != null && args.length > 0){
			for(Object param : args){
				logger.debug(">>> param[" + (param == null ? Object.class : param.getClass()) + "] : " + param);
			}
		}
		Object result = method.invoke(target, args);
		logger.debug(">>> result[boolean] : " + result);
		return result;
	}

}
